import { action, observable } from 'mobx';
import { message, Modal } from 'antd';
import * as API from '../../apis/performance/reportShareApi';

/**
 * 绩效-考核结果分析共享设置
 * jiansong 2019/6/11
 */
export default class ReportShareStore {

    @observable loading = true; //加载中
    @observable showUpdataDialog = false; //是否展示新增对话框
    @observable data = []; //表单数据
    @observable selectedRowKeys = []; //选中的行
    @observable hasRight = false; // 判断用户是有权限查看当前页面： 没有权限渲染无权限页面，有权限渲染数据
    msg = "";
    sharetypeOption = [//共享类型
        { key: "1", showname: "人力资源" },
        { key: "2", showname: "部门" },
        { key: "3", showname: "角色" },
        { key: "4", showname: "所有人" }
    ];
    sharelevelOption = [//共享级别
        { key: "1", showname: "个人" },
        { key: "2", showname: "同部门" },
        { key: "3", showname: "同分部" },
        { key: "4", showname: "总部" },
        { key: "5", showname: "同部门及下级部门" },
        { key: "6", showname: "多部门" },
        { key: "7", showname: "同分部及下级分部" },
        { key: "8", showname: "多分部" },
        { key: "9", showname: "多人员" }
    ];
    rankOption = [//级别
        { key: "0", showname: "部门" },
        { key: "1", showname: "分部" },
        { key: "2", showname: "总部" },
    ];

    @observable form = {
        operation: "add", //操作类型
        reportid: "1", //默认值，未知含义
        sharetype: "1", //共享类型
        relatedshareid: "", //共享对象id
        seclevel: "10", //安全级别
        rolelevel: "0", //角色级别
        sharelevel: "1", //共享级别
        mutiids: "", //共享级别对象id
        relatedshareData: [], //共享对象
        mutiData: [], //共享级别对象
    };

    @action("初始化操作")
    doInit = () => {
        this.getTableDatas();
    }

    @action("数据改变")
    onFormChange = (params) => {
        for (let key in params) {
            this.form[key] = params[key];
        }
    }

    @action("渲染table数据")
    getTableDatas = (params) => {
        this.loading = true;
        API.reportShareListApi(params).then(action(res => {
            if (res.api_status) {
                this.data = res.datas;
                this.hasRight = res.hasRight;
                if(this.hasRight){
                    this.msg = "1";
                }else{
                    this.msg = "0";
                }
            } else {
                message.error(res.msg || '接口调用失败！')
            }
            this.loading = false;
        }));
    };

    @action("切换编辑框是否展示")
    changeShowUpdataDialog = () => {
        this.showUpdataDialog = !this.showUpdataDialog
    };

    @action("表单关闭的时候清空表单内容")
    closeForm = () => {
        this.initForm()
        this.changeShowUpdataDialog()
    };

    @action("初始化添加表单")
    initAddForm = () => {
        this.form.operation = "add"
        this.changeShowUpdataDialog()
    };

    @action("初始化表单内容")
    initForm = () => {
        this.form = {
            operation: "add", //操作类型
            reportid: "1", //默认值，未知含义
            sharetype: "1", //共享类型
            relatedshareid: "", //共享对象id
            seclevel: "10", //安全级别
            rolelevel: "0", //角色级别
            sharelevel: "1", //共享级别
            mutiids: "", //共享级别对象id
            relatedshareData: [], //共享对象
            mutiData: [], //共享级别对象
        }
    }

    @action("考核指标项-操作")
    operation = () => {
        if (this.checkParams()) {
            this.loading = true;
            API.reportShareOperationApi(this.form).then(action(res => {
                if (res.api_status) {
                    message.success("操作成功");
                    this.changeShowUpdataDialog()
                    this.getTableDatas()
                    this.initForm()
                } else {
                    message.error(res.msg || '接口调用失败！')
                }
                this.loading = false;
            }));
        }
    }

    //校验表单参数
    checkParams = () => {
        let { sharetype, sharelevel, relatedshareid, mutiids } = this.form;
        if (sharetype == 1 || sharetype == 2 || sharetype == 3) {
            if (relatedshareid == '') {
                message.error("请完成必填项!")
                return false;
            }
        }
        if (sharelevel == 6 || sharelevel == 8 || sharelevel == 9) {
            if (mutiids == '') {
                message.error("请完成必填项!")
                return false;
            }
        }
        return true;
    }

    @action("考核指标项-操作删除")
    operationDelOne = (id) => {
        Modal.confirm({
            title: '信息确认',
            content: '确定删除数据?',
            onOk: () => {
                let params = {
                    ids: id,
                    operation: "del",
                }
                this.loading = true;
                API.reportShareOperationApi(params).then(action(res => {
                    if (res.api_status) {
                        if (!res.hasRight) {
                            message.error('对不起，您暂时没有权限！');
                        } else {
                            message.success("操作成功");
                            this.getTableDatas()
                        }
                    } else {
                        message.error(res.msg || '接口调用失败！')
                    }
                    this.loading = false;
                }));

            },
        });
    }

    //考核指标项-操作删除
    @action
    operationDel = () => {
        if (this.selectedRowKeys.length > 0) {
            Modal.confirm({
                title: '信息确认',
                content: '确定删除已选数据?',
                onOk: () => {
                    let params = {
                        ids: this.selectedRowKeys.join(","),
                        operation: "del",
                    }
                    this.loading = true;
                    API.reportShareOperationApi(params).then(action(res => {
                        if (res.api_status) {
                            this.selectedRowKeys = [];
                            message.success("操作成功");
                            this.getTableDatas()
                        } else {
                            message.error(res.msg || '接口调用失败！')
                        }
                        this.loading = false;
                    }));

                },
            });
        } else {
            Modal.warning({
                title: '系统信息',
                content: '请选择要删除的数据!'
            });
        }
    }



}